﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace SilverlightWebcamEncoder.Behaviors
{
    public class BindableVisualStateBehavior : Behavior<Control>
    {
        public string VisualState
        {
            get { return (string)GetValue(VisualStateProperty); }
            set { SetValue(VisualStateProperty, value); }
        }

        public static readonly DependencyProperty VisualStateProperty =
            DependencyProperty.Register("VisualState", typeof(string), typeof(BindableVisualStateBehavior), new PropertyMetadata("", OnVisualStateChangedStatic));


        private static void OnVisualStateChangedStatic(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as BindableVisualStateBehavior).OnVisualStateChanged(e);
        }

        private void OnVisualStateChanged(DependencyPropertyChangedEventArgs e)
        {
            string state = (string)e.NewValue;

            if (string.IsNullOrEmpty(state))
                return;

            VisualStateManager.GoToState(AssociatedObject, state, true);
        }
    }
}
